package br.com.hstar.client.usuario;

import br.com.hstar.client.ServiceFactory;
import br.com.hstar.client.util.FormPanel;
import br.com.hstar.client.util.HstarDialogBox;
import br.com.hstar.client.util.HstarUserException;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class UsuarioEditPanel extends FormPanel {

	private Button incluir;
	private Button cancelar;
	private Button excluir;
	
	private TextBox nome;
	private TextBox login;
	private PasswordTextBox senha;
	private ListBox permissao;
	
	private UsuarioUI usuarioUI;

	public UsuarioEditPanel(String _title) {
		super(_title);
		usuarioUI = new UsuarioUI();
		init(true);
	}
	
	public UsuarioEditPanel(String _title, Long id) {
		super(_title);
		usuarioUI = new UsuarioUI();
		usuarioUI.setId(id);
		init(false);
		ServiceFactory.createUsuarioService().retrieveById(id, new RetrieveAsyncCallback());
	}
	
	
	
	//Methods -------------------------------------------------------------------
	
	private void init(boolean isNew) {

		nome 	= addTextBox("Nome:", 10, 20, "");
		login 	= addTextBox("Login:", 10, 20, "");
		senha 	= addPassTextBox("Senha:", "");
		permissao 	= addListBox("Permissão:", true);
		preenchePermissoes();
		
		if(isNew) {
			incluir  = addButton("Incluir");
			cancelar = addButton("Cancelar");
		} else {
			incluir  = addButton("Alterar");
			cancelar = addButton("Cancelar");
			excluir = addButton("Excluir");
			excluir.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					ServiceFactory.createUsuarioService().delete(usuarioUI.getId(), new DeleteAsyncCallback());
				}
			});
		}
		
		incluir.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				 try {
					copyData();
					ServiceFactory.createUsuarioService().saveOrUpdate(usuarioUI, new SaveAsyncCallback());
				} catch (HstarUserException e) {
					new HstarDialogBox("Usuario", e.getMessage());					
				}
				 
			}
		});
		cancelar.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				RootPanel.get("boxContent").clear();
				RootPanel.get("boxContent").add(new UsuarioListPanel("Usuario"));
			}
		});
		
	}

	private void copyData() throws HstarUserException {
		String msgErro = "";
		
		if(nome.getText().trim() == ""){
			msgErro += " - Digite o nome do Usuario <br>";
		}
		if(login.getText().trim() == ""){
			msgErro += " - Digite o login Usuario <br>";
		}
		if(senha.getText().trim() == ""){
			msgErro += " - Digite a senha do Usuario <br>";			
		}
		if(msgErro != "")
		{
			throw new HstarUserException(msgErro);			
		}

		usuarioUI.setNome(nome.getText());
		usuarioUI.setLogin(login.getText());
		usuarioUI.setSenha(senha.getText());
		usuarioUI.setPermissao(new Integer(permissao.getValue(permissao.getSelectedIndex())));
	}

	// INNER CLASSES ----------------------------------------------------------------
	private class SaveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			if(caught instanceof HstarUserException)
			{					
				new HstarDialogBox("Usuario",caught.getMessage());
			}
		}

		public void onSuccess(Object result) {
			RootPanel.get("boxContent").clear();
			RootPanel.get("boxContent").add(new UsuarioListPanel("Usuario"));
		}
	}
	
	private class DeleteAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO - Usuario");
		}

		public void onSuccess(Object result) {
			RootPanel.get("boxContent").clear();
			RootPanel.get("boxContent").add(new UsuarioListPanel("Usuario"));
		}
	}
	
	//carrega os campos. Usado para quando está editando
	private class RetrieveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO - Usuario");
		}

		public void onSuccess(Object result) {
			UsuarioUI usu = (UsuarioUI)result;
			nome.setText(usu.getNome());
			login.setText(usu.getLogin());
			senha.setText(usu.getSenha());
			preenchePermissoes();
			permissao.setSelectedIndex(usu.getPermissao().intValue());
		}
	}
	
	//preenche a lista de permissoes
	public void preenchePermissoes() {
		permissao.clear();
		permissao.addItem("ADMINISTRADOR", "0");
		permissao.addItem("RECEPCIONISTA", "1");
		permissao.addItem("CAMAREIRA", "2");
	}
}      